import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { getUserInfoData } from "../../api/user";
import { getCity } from "../../api/city";
import { dataTypes } from "../../types/store";

const defaultData:dataTypes = {
    userData:null,
    cityData:[], //城市数据保存
    cityDw:'未定位',
    buzhou:0,
    MSData:{
        address:'',
        campany:'',
        date:'',
        level:[],
        imgs1:[],
        imgs:[],
        date1:'',
        audioSrc1:undefined,
        audioSrc:'',
        tags:[],
        likes:0,
        collects:0,
        pings:0 ,
        content:''
    },
    fenleiDW:'-1', //分类指定位置
    isLuYin:'has', //信息是否录音
    typeLev:'0',    //选择信息难度
    searchList:[],  //保存搜素历史记录
    pings:0,    //个人中心评论数量
    likes:0,     //个人中心点赞数量
    collect:0,  //个人中心收藏数量
}

const datas = createSlice({
    name:'datas',
    initialState:defaultData,
    reducers:{
        changeUserData(state,{payload}){
            state.userData = payload
        },
        changeCityData(state,{payload}){
            state.cityData = payload
        },
        chnageDw(state,{payload}){
            state.cityDw = payload
        },
        changeBu(state,{payload}){
            state.buzhou = payload
        },
        changeMSData(state,{payload}){
            state.MSData = {...state.MSData,...payload}
            console.log(state.MSData);
            
        },
        clearData(state){
            state.MSData = {
                address:'',
                campany:'',
                date:'',
                level:[],
                imgs1:[],
                imgs:[],
                date1:'',
                audioSrc1:undefined,
                audioSrc:'',
                tags:[],
                likes:0,
                collects:0,
                pings:0 ,
                content:''
            }
        },
        changeFL(state,{payload}){
            state.fenleiDW = payload;
        },
        changeLY(state,{payload}){
            state.isLuYin = payload
            console.log(payload);
        },
        changeTypeLev(state,{payload}){
            state.typeLev = payload
        },
        chnageSearch(state,{payload}){
            state.searchList = payload
        },
        changeShuLiang(state,{payload}){
            state.pings = payload.pings;
            state.collect = payload.collect;
            state.likes = payload.likes;

        }
    },
    extraReducers:(b)=>{
        b
        .addCase(getUserInfo.fulfilled,(state,actions)=>{
            state.userData = actions.payload
            console.log(actions);
            
        })
        .addCase(getCityInfo.fulfilled,(state,{payload})=>{
            state.cityData = payload
        })
    }
})


// 方法
export const {
    changeUserData,
    changeCityData,
    chnageDw,
    changeBu,
    changeMSData,
    changeFL,
    changeLY,
    changeTypeLev,
    chnageSearch,
    clearData,
    changeShuLiang

} = datas.actions

export const getUserInfo  = createAsyncThunk('userInfos',async()=>{
    let res = await getUserInfoData();
    return res.result
})

export const getCityInfo = createAsyncThunk('cityInfos',async()=>{
    let res = await getCity({},{'X-Host': 'mall.film-ticket.city.list'})
    return res.data.cities
})

// 组件本身
export default datas.reducer